package controllers
{
	import models.Diagram;
	import models.Project;
	import models.RESTResponse;
	import models.UMLClass;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Application;
	
	public class DiagramManager
	{
		private var _current:Diagram;
		
		public function DiagramManager() {
			_current 	= null;
		}
		
		public function wakeUp() : void {
			getDiagram(_current.oid);
		}
		
		public function getDiagram(oid:int) : void {
			HTTPController.GET('/diagrams/diagram/' + oid, null, function (response:RESTResponse) : void {
				if(response.success()) {
					var diagram:Diagram = new Diagram();
					diagram.setXML(response.dataNode());
					_current = diagram;
					drawDiagram();
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}

		public function createDiagram(diagram:Diagram, callback:Function) : void {
			HTTPController.POST('/diagrams', diagram.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					var diagram:Diagram = new Diagram();
					diagram.setXML(response.dataNode());
					_current = diagram;
					drawDiagram();
					callback(response);
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
		
		public function updateDiagram(diagram:Diagram, callback:Function) : void {
			HTTPController.PUT('/diagrams/diagram', diagram.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					var diagram:Diagram = new Diagram();
					diagram.setXML(response.dataNode());
					_current = diagram;
					drawDiagram();
					callback(response);
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});	
		}
		
		public function findByProject(project:Project, callback:Function) : void {
			HTTPController.GET('/diagrams/diagram/project/' + project.oid, null, callback);
		}
		
		public function saveDiagram(projectId:int, type:String, name:String) : void {
			if(_current == null) {
				createDiagram(new Diagram(projectId, type, name), function (response:RESTResponse) : void {
					Application.application.projectManager.updateList();
				});
			} else {
				_current.projectId = projectId;
				_current.type = type;
				_current.name = name;
				updateDiagram(_current, function (response:RESTResponse) : void {
					Application.application.projectManager.updateLis();
				});
				_current = null;
			}
		}
		
		public function set currentDiagram(diagram:Diagram) : void {
			_current = diagram;
		} 
		
		public function get currentDiagram() : Diagram {
			return _current;
		} 
		
		/* Diagrams */
		
		public function drawDiagram() : void {
			var app:Object = Application.application;
			var diagram:XML = app.makeDiagramXML(_current.layout);
			app.drawDiagram(diagram);
		}
		
		public function setClassInDiagram(umlClass:UMLClass, x:int, y:int) : void {
			var layout:XML = _current.layout;
			if(layout.umlclasses.umlclass.(attribute('oid') == umlClass.oid).length() == 0) {
				layout.umlclasses.appendChild(new XML('<umlclass oid="' + umlClass.oid + '" x="' + x + '" y="' + y + '"/>'));
			} else {
				layout.umlclasses.umlclass.(attribute('oid') == umlClass.oid).@x = x;
				layout.umlclasses.umlclass.(attribute('oid') == umlClass.oid).@y = y; 
			}
			_current.layout = layout;
			
			var self:DiagramManager = this;
			updateDiagram(_current, function (response:RESTResponse) : void {
				if(response.success()) {
					var diagram:Diagram = new Diagram();
					diagram.setXML(response.dataNode());
					self.currentDiagram = diagram;
					//self.drawDiagram();
				}
			});
		}
		
		private function createTMPXML(classes:ArrayCollection) : XML {
			var xml:XML = 
			<diagram>
				<umlclasses />
			</diagram>;
			
			var i:int = 0;
			for each(var umlClass:UMLClass in classes) {
				++ i;
				xml.umlclasses.appendChild(new XML('<umlclass oid="' + umlClass.oid + '" x="' + (i * 100) + '" y="' + (i * 100) + '"/>'));
			}
			
			return xml;
		}
	}
}